home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / gnucdiff / context.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-16  |  8.8 KB  |  306 lines

  1. /* Context-format output routines for GNU DIFF.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21. #include "diff.h"
  22. #include "regex.h"
  23.  
  24. static void print_context_hunk ();
  25. static struct change *find_hunk ();
  26. static void mark_ignorable ();
  27. static int find_function ();
  28.  
  29. /* Last place find_function started searching from.  */
  30. static int find_function_last_search;
  31.  
  32. /* The value find_function returned when it started searching there.  */
  33. static int find_function_last_match;
  34.  
  35. /* Print a header for a context diff, with the file names and dates.  */
  36.  
  37. void
  38. print_context_header (inf)
  39.      struct file_data *inf;
  40. {
  41.   fprintf (outfile, "*** %s\t%s", inf[0].name,
  42.        ctime (&inf[0].stat.st_mtime));
  43.   fprintf (outfile, "--- %s\t%s", inf[1].name,
  44.        ctime (&inf[1].stat.st_mtime));
  45. }
  46.  
  47. /* Print an edit script in context format.  */
  48.  
  49. void
  50. print_context_script (script)
  51.      struct change *script;
  52. {
  53.   if (ignore_blank_lines_flag || ignore_regexp)
  54.     mark_ignorable (script);
  55.   else
  56.     {
  57.       struct change *e;
  58.       for (e = script; e; e = e->link)
  59.     e->ignore = 0;
  60.     }
  61.  
  62.   find_function_last_search = 0;
  63.   find_function_last_match = -1;
  64.  
  65.   print_script (script, find_hunk, print_context_hunk);
  66. }
  67.  
  68. /* Print a pair of line numbers with a comma, translated for file FILE.
  69.    If the second number is smaller, use the first in place of it.
  70.  
  71.    Args A and B are internal line numbers.
  72.    We print the translated (real) line numbers.  */
  73.  
  74. static void
  75. print_context_number_range (file, a, b)
  76.      struct file_data *file;
  77.      int a, b;
  78. {
  79.   int trans_a, trans_b;
  80.   translate_range (file, a, b, &trans_a, &trans_b);
  81.  
  82.   /* Note: we can have B < A in the case of a range of no lines.
  83.      In this case, we should print the line number before the range,
  84.      which is B.  */
  85.   if (trans_b >= trans_a)
  86.     fprintf (outfile, "%d,%d", trans_a, trans_b);
  87.   else
  88.     fprintf (outfile, "%d", trans_b);
  89. }
  90.  
  91. /* Print a portion of an edit script in context format.
  92.    HUNK is the beginning of the portion to be printed.
  93.    The end is marked by a `link' that has been nulled out.
  94.  
  95.    Prints out lines from both files, and precedes each
  96.    line with the appropriate flag-character.  */
  97.  
  98. static void
  99. print_context_hunk (hunk)
  100.      struct change *hunk;
  101. {
  102.   int first0, last0, first1, last1, show_from, show_to, i;
  103.   struct change *next;
  104.   char *prefix;
  105.   int lastline;
  106.   char *function;
  107.   int function_length;
  108.  
  109.   /* Determine range of line numbers involved in each file.  */
  110.  
  111.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  112.  
  113.   if (!show_from && !show_to)
  114.     return;
  115.  
  116.   /* Include a context's width before and after.  */
  117.  
  118.   first0 = max (first0 - context, 0);
  119.   first1 = max (first1 - context, 0);
  120.   last0 = min (last0 + context, files[0].buffered_lines - 1);
  121.   last1 = min (last1 + context, files[1].buffered_lines - 1);
  122.  
  123.   /* If desired, find the preceding function definition line in file 0.  */
  124.   function = 0;
  125.   if (function_regexp)
  126.     find_function (&files[0], first0, &function, &function_length);
  127.  
  128.   /* If we looked for and found a function this is part of,
  129.      include its name in the header of the diff section.  */
  130.   fprintf (outfile, "***************");
  131.  
  132.   if (function)
  133.     {
  134.       fprintf (outfile, " ");
  135.       fwrite (function, 1, min (function_length - 1, 40), outfile);
  136.     }
  137.  
  138.   fprintf (outfile, "\n*** ");
  139.   print_context_number_range (&files[0], first0, last0);
  140.   fprintf (outfile, " ****\n");
  141.  
  142.   if (show_from)
  143.     {
  144.       next = hunk;
  145.  
  146.       for (i = first0; i <= last0; i++)
  147.     {
  148.       /* Skip past changes that apply (in file 0)
  149.          only to lines before line I.  */
  150.  
  151.       while (next && next->line0 + next->deleted <= i)
  152.         next = next->link;
  153.  
  154.       /* Compute the marking for line I.  */
  155.  
  156.       prefix = " ";
  157.       if (next && next->line0 <= i)
  158.         /* The change NEXT covers this line.
  159.            If lines were inserted here in file 1, this is "changed".
  160.            Otherwise it is "deleted".  */
  161.         prefix = (next->inserted > 0 ? "!" : "-");
  162.  
  163.       print_1_line (prefix, &files[0].linbuf[i]);
  164.     }
  165.     }
  166.  
  167.   fprintf (outfile, "--- ");
  168.   print_context_number_range (&files[1], first1, last1);
  169.   fprintf (outfile, " ----\n");
  170.  
  171.   if (show_to)
  172.     {
  173.       next = hunk;
  174.  
  175.       for (i = first1; i <= last1; i++)
  176.     {
  177.       /* Skip past changes that apply (in file 1)
  178.          only to lines before line I.  */
  179.  
  180.       while (next && next->line1 + next->inserted <= i)
  181.         next = next->link;
  182.  
  183.       /* Compute the marking for line I.  */
  184.  
  185.       prefix = " ";
  186.       if (next && next->line1 <= i)
  187.         /* The change NEXT covers this line.
  188.            If lines were deleted here in file 0, this is "changed".
  189.            Otherwise it is "inserted".  */
  190.         prefix = (next->deleted > 0 ? "!" : "+");
  191.  
  192.       print_1_line (prefix, &files[1].linbuf[i]);
  193.     }
  194.     }
  195. }
  196.  
  197. /* Scan a (forward-ordered) edit script for the first place that at least
  198.    2*CONTEXT unchanged lines appear, and return a pointer
  199.    to the `struct change' for the last change before those lines.  */
  200.  
  201. static struct change *
  202. find_hunk (start)
  203.      struct change *start;
  204. {
  205.   struct change *prev;
  206.   int top0, top1;
  207.   int thresh;
  208.  
  209.   do
  210.     {
  211.       /* Computer number of first line in each file beyond this changed.  */
  212.       top0 = start->line0 + start->deleted;
  213.       top1 = start->line1 + start->inserted;
  214.       prev = start;
  215.       start = start->link;
  216.       /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  217.      but only CONTEXT if one is ignorable.  */
  218.       thresh = ((prev->ignore || (start && start->ignore))
  219.         ? context
  220.         : 2 * context);
  221.       /* It is not supposed to matter which file we check in the end-test.
  222.      If it would matter, crash.  */
  223.       if (start && start->line0 - top0 != start->line1 - top1)
  224.     abort ();
  225.     } while (start
  226.          /* Keep going if less than THRESH lines
  227.         elapse before the affected line.  */
  228.          && start->line0 < top0 + thresh);
  229.  
  230.   return prev;
  231. }
  232.  
  233. /* Set the `ignore' flag properly in each change in SCRIPT.
  234.    It should be 1 if all the lines inserted or deleted in that change
  235.    are ignorable lines.  */
  236.  
  237. static void
  238. mark_ignorable (script)
  239.      struct change *script;
  240. {
  241.   while (script)
  242.     {
  243.       struct change *next = script->link;
  244.       int first0, last0, first1, last1, deletes, inserts;
  245.  
  246.       /* Turn this change into a hunk: detach it from the others.  */
  247.       script->link = 0;
  248.  
  249.       /* Determine whether this change is ignorable.  */
  250.       analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
  251.       /* Reconnect the chain as before.  */
  252.       script->link = next;
  253.  
  254.       /* If the change is ignorable, mark it.  */
  255.       script->ignore = (char)(!deletes && !inserts);
  256.  
  257.       /* Advance to the following change.  */
  258.       script = next;
  259.     }
  260. }
  261.  
  262. /* Find the last function-header line in FILE prior to line number LINENUM.
  263.    This is a line containing a match for the regexp in `function_regexp'.
  264.    Store the address of the line text into LINEP and the length of the
  265.    line into LENP.
  266.    Do not store anything if no function-header is found.  */
  267.  
  268. static int
  269. find_function (file, linenum, linep, lenp)
  270.      struct file_data *file;
  271.      int linenum;
  272.      char **linep;
  273.      int *lenp;
  274. {
  275.   int i = linenum;
  276.   int last = find_function_last_search;
  277.   find_function_last_search = i;
  278.  
  279.   while (--i >= last)
  280.     {
  281.       /* See if this line is what we want.  */
  282.  
  283.       if (0 <= re_search (&function_regexp_compiled,
  284.               files[0].linbuf[i].text,
  285.               files[0].linbuf[i].length,
  286.               0, files[0].linbuf[i].length,
  287.               0))
  288.     {
  289.       *linep = files[0].linbuf[i].text;
  290.       *lenp = files[0].linbuf[i].length;
  291.       find_function_last_match = i;
  292.       return 1;
  293.     }
  294.     }
  295.   /* If we search back to where we started searching the previous time,
  296.      find the line we found last time.  */
  297.   if (find_function_last_match >= 0)
  298.     {
  299.       i = find_function_last_match;
  300.       *linep = files[0].linbuf[i].text;
  301.       *lenp = files[0].linbuf[i].length;
  302.       return 1;
  303.     }
  304.   return 0;
  305. }
  306.